package cn.demo.day5;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Robot;

/*
 * 游戏地址：http://www.4399.com/flash/135255_3.htm
 */
public class RobotText {
	public static void main(String[] args) throws AWTException, InterruptedException {
		// 程序运行前延时3000ms
		Thread.sleep(3000);
		int a = 1;
		// 无限while循环
		while (a <= 100) {
			// 每次按键间隔100ms
			Thread.sleep(100);
			for (int i = 1; i <= 4; i++) {
				Robot robot = new Robot();
				// 获取屏幕某一像素的颜色
				Color pixelColor = robot.getPixelColor(100 * i + 600, 520);
				// 这里黑方块的三原色是（51,51,51）
				if (pixelColor.getRed() == 51) {
					switch (i) {
					// 模拟按键：例如D键的ascii码是68
					case 1:
						robot.keyPress(68);
						robot.keyRelease(68);
						break;
					case 2:
						robot.keyPress(70);
						robot.keyRelease(70);
						break;
					case 3:
						robot.keyPress(74);
						robot.keyRelease(74);
						break;
					case 4:
						robot.keyPress(75);
						robot.keyRelease(75);
						break;
					}
				}
			}
		}
	}
}
